package main

import (
	"encoding/json"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"github.com/panthesingh/goson"
	"io/ioutil"
	"net/http"
	"regexp"
	"strconv"
	"strings"
	"time"
)

func sohuListHandle(src string, categoryId int, childId int) {

	resp, err := http.Get(src)
	if err != nil {
		trace(fmt.Sprintf("http get failed : %v", err))
		return
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)

	var maxExp = regexp.MustCompile(`var maxPage = (\d+);`)
	//var showPageExp = regexp.MustCompile(`var showPages = (\d+);`)
	var finish = false
	var min = 0
	var url string

	maxPage, _ := strconv.Atoi(maxExp.FindStringSubmatch(string(body))[1])
	//showPage, _ := strconv.Atoi(showPageExp.FindStringSubmatch(string(body))[1])

	//if maxPage > showPage {
	//	min = maxPage - showPage + 1
	//} else {
	//	min = 1
	//}

	// 改为只采最新的2页
	min = maxPage - 2
	if min <= 0 {
		min = 1
	}

	urlSuffix := strings.Split(src, ".shtml")[0]
	pageUrl := urlSuffix + "_%d.shtml"

	time.Sleep(3 * time.Second)
	for i := maxPage; i >= min; i-- {

		if i == maxPage {
			url = src
		} else {
			url = fmt.Sprintf(pageUrl, i)
		}
		fmt.Println(url)

		doc, err := goquery.NewDocument(url)
		if err != nil {
			trace(fmt.Sprintf("%s createDoc failed : %v", url, err))
			continue
		}

		doc.Find(".list14 ul li").Each(func(j int, s *goquery.Selection) {
			href, _ := s.Find("a").Attr("href")

			article, _ := getArticle(href)
			if article.ArticleId > 0 {
				trace(fmt.Sprintf("搜狐汽车 finish : %s", href))
				finish = true
			}

			if len(href) > 0 && finish == false {
				art := new(ClcwArticle)
				art.CategoryId = categoryId
				art.ChildId = childId
				art.FromUrl = href
				art.FromName = "搜狐汽车"
				jsonStr, err := json.Marshal(art)
				if err != nil {
					trace(fmt.Sprintf("failed: %v", href))
					trace(fmt.Sprintf("json Marshal failed : %v", err))
				} else {
					trace(fmt.Sprintf("lpush : %v", href))
					lpush(conf.Redis.ArticleQueue, jsonStr)
				}
				time.Sleep(time.Duration(conf.Crawler.Interval) * time.Second)
			}
		})

		if finish == true {
			return
		}
	}
}

func sohuDetailHandle(art *ClcwArticle) {

	doc, err := createDoc(art.FromUrl)
	if err != nil {
		trace(fmt.Sprintf("%s createDoc failed : %v", art.FromUrl, err))
		return
	}
	art.ArticleTitle = doc.Find("#contentA h1").Text()
	art.ArticleTitle = strReplace(art.ArticleTitle)

	art.Author = doc.Find(".source").Contents().Last().Text()
	art.Author = strReplace(art.Author)

	//pushTime := doc.Find("#contentA.time").Text()
	//art.CreateTime, _ = formatTime(pushTime)

	art.CreateTime = int(time.Now().Unix())
	art.ArticleContent, _ = doc.Find("#contentText").Html()

	if len(art.ArticleTitle) <= 0 || len(art.ArticleContent) <= 0 {
		return
	}
	art2utf8(art, "GB18030")
	art.Status = 2
	art.Type = 2
	art.FromName = "搜狐汽车"
	art.Author = strings.Replace(art.Author, "作者：", "", -1)
	affected, err := engine.Table("clcw_article_crawler").Insert(art)
	if err != nil {
		trace(fmt.Sprintf("article insert failed: %v", err))
	} else {
		trace(fmt.Sprintf("article insert %d : %s", affected, art.ArticleTitle))
	}
}

func sohuUsedCarListHandle(src string, categoryId int, childId int, start int, end int) {

	for i := start; i <= end; i++ {
		url := fmt.Sprintf(src, i, time.Now().UnixNano())
		fmt.Println(url)
		resp, err := http.Get(url)
		if err != nil {
			trace(fmt.Sprintf("http get failed : %v", err))
			continue
		}
		defer resp.Body.Close()
		body, err := ioutil.ReadAll(resp.Body)

		jsonObj, err := goson.Parse(body)
		if err != nil {
			trace(fmt.Sprintf("ParseJson json %s failed: %v", src, err))
			continue
		}

		for j := 0; j < jsonObj.Get("result").Len(); j++ {
			href := jsonObj.Get("result").Index(j).Get("url").String()
			img := jsonObj.Get("result").Index(j).Get("picurl").String()

			article, _ := getArticle(href)
			if article.ArticleId > 0 {
				trace(fmt.Sprintf("搜狐二手车 finish : %s", href))
				return
			}

			if len(href) > 0 {
				art := new(ClcwArticle)
				art.CategoryId = categoryId
				art.ChildId = childId
				art.ArticleFocus = img
				art.ArticleThumb = img
				art.FromUrl = href
				art.FromName = "搜狐二手车"
				jsonStr, err := json.Marshal(art)
				if err != nil {
					trace(fmt.Sprintf("failed: %v", href))
					trace(fmt.Sprintf("json Marshal failed : %v", err))
				} else {
					trace(fmt.Sprintf("lpush : %v", href))
					lpush(conf.Redis.ArticleQueue, jsonStr)
				}
				time.Sleep(time.Duration(conf.Crawler.Interval) * time.Second)
			}
		}
	}
}

func sohuUsedCarDetailHandle(art *ClcwArticle) {

	doc, err := createDoc(art.FromUrl)
	if err != nil {
		trace(fmt.Sprintf("%s createDoc failed : %v", art.FromUrl, err))
		return
	}
	art.ArticleTitle = doc.Find(".news-content h1").Text()
	art.ArticleTitle = strReplace(art.ArticleTitle)

	art.Author = doc.Find(".source").Contents().Last().Text()
	art.Author = strReplace(art.Author)

	//pushTime := doc.Find("#contentText.time").Text()
	//art.CreateTime, _ = formatTime(pushTime)

	art.CreateTime = int(time.Now().Unix())
	art.ArticleContent, _ = doc.Find("#contentText").Html()

	if len(art.ArticleTitle) <= 0 || len(art.ArticleContent) <= 0 {
		return
	}
	art2utf8(art, "GB18030")
	art.Status = 2
	art.Type = 2
	art.FromName = "搜狐二手车"
	art.Author = strings.Replace(art.Author, "作者：", "", -1)
	affected, err := engine.Table("clcw_article_crawler").Insert(art)
	if err != nil {
		trace(fmt.Sprintf("article insert failed: %v", err))
	} else {
		trace(fmt.Sprintf("article insert %d : %s", affected, art.ArticleTitle))
	}
}

func sohuHandle(url string, char string, categoryId int, childId int) {

	doc, err := createDoc(url)
	if err != nil {
		trace(fmt.Sprintf("%s createDoc failed : %v", url, err))
	}

	doc.Find(".list-box li").Each(func(i int, s *goquery.Selection) {
		art := new(ClcwArticle)
		//title := s.Find(".content-title a").Text()
		href, _ := s.Find(".content-title a").Attr("href")
		img, _ := s.Find(".content-pic img").Attr("src")
		art.CategoryId = categoryId
		art.ChildId = childId
		art.ArticleFocus = img
		art.ArticleThumb = img
		art.FromUrl = href
		sohuZiDetailHandle(href, char, art)
	})
}

func sohuBatchHandle(src string, char string, categoryId int, childId int, start int, end int) {

	for i := start; i <= end; i++ {
		url := fmt.Sprintf(src, i)
		doc, err := createDoc(url)
		if err != nil {
			trace(fmt.Sprintf("%s createDoc failed : %v", url, err))
		}

		doc.Find(".list-box li").Each(func(i int, s *goquery.Selection) {
			art := new(ClcwArticle)
			//title := s.Find(".content-title a").Text()
			href, _ := s.Find(".content-title a").Attr("href")
			img, _ := s.Find(".content-pic img").Attr("src")
			art.CategoryId = categoryId
			art.ChildId = childId
			art.ArticleFocus = img
			art.ArticleThumb = img
			art.FromUrl = href
			sohuZiDetailHandle(href, char, art)
		})

	}
}

func sohuZiDetailHandle(url string, char string, art *ClcwArticle) {
	doc, err := createDoc(url)
	if err != nil {
		trace(fmt.Sprintf("%s createDoc failed : %v", url, err))
	}
	art.ArticleTitle = doc.Find("div.news-title h1").Text()
	art.Author = doc.Find("span.writer").Text()
	//pushTime := doc.Find("span.time").Text()
	//art.CreateTime, _ = int(formatTime(pushTime))
	art.CreateTime = int(time.Now().Unix())
	art.ArticleContent, _ = doc.Find("#contentText").Html()
	art.ArticleDesc = doc.Find("#description").Text()
	art2utf8(art, char)
	if len(art.ArticleTitle) > 0 {
		b, _ := engine.Table("clcw_article_crawler").Get(&ClcwArticle{ArticleTitle: art.ArticleTitle})
		if !b {
			art.Status = 2
			art.Type = 2
			art.FromName = "搜狐自媒体"
			affected, err := engine.Table("clcw_article_crawler").Insert(art)
			if err != nil {
				fmt.Println(err)
			}
			fmt.Println(affected, art.ArticleTitle)
		} else {
			fmt.Println(0, art.ArticleTitle)
		}
	}

}
